n = int(input())
boards = list(map(int, input().split()))
max_val = 0
for a in range(n):  # 枚举左边的板子
    b = a + 1
    c = n - 1
    while a < b < c:
        left_val = min(boards[a], boards[b]) * (b - a)
        right_val = min(boards[b], boards[c]) * (c - b)

        max_val = max(max_val, left_val + right_val)
        if boards[b] < boards[c]:
            b += 1
        else:
            c -= 1
print(max_val)
